package com.antler.smt.admin.service.impl;

import com.antler.smt.admin.mapper.*;
import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.constant.OrderConstant;
import com.antler.smt.entity.OrderEntity;
import com.antler.smt.entity.PurchaserAccountEntity;
import com.antler.smt.entity.RefundRecordEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.RefundReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.RefundService;
import com.antler.smt.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zhangyi
 * @date 2018/6/6 18:44
 */
@Service
public class RefundServiceImpl implements RefundService {

    @Resource
    private OrderEntityMapper orderEntityMapper;

    @Resource
    private RefundRecordEntityMapper refundRecordEntityMapper;

    @Resource
    private RefundRecordInfoMapper refundRecordInfoMapper;

    @Resource
    private PurchaserAccountEntityMapper purchaserAccountEntityMapper;
    @Resource
    private PurchaserAccountInfoMapper purchaserAccountInfoMapper;


    /**
     * 退款审核处理
     *
     * @param refundReq
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result refund(RefundReq refundReq) {
        Result result = null;
        Integer auditStatus = refundReq.getAuditStatus();
        RefundRecordEntity refundRecordEntity = new RefundRecordEntity();
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setId(refundReq.getOrderId());

        refundRecordEntity.setOrderId(refundReq.getOrderId());
        refundRecordEntity.setAuditRefundTime(DateUtil.stampToDate(System.currentTimeMillis()));
        refundRecordEntity.setAuditUserId(refundReq.getAuditUserId());

        RefundRecordEntity refundRecordEntity1 = refundRecordInfoMapper.selectRefundByOrderId(refundReq.getOrderId());
        //订单状态
        int i = 0;
        int p = 0;
        int o = 0;
        if(auditStatus.equals(OrderConstant.ORDER_REFUND_PASS)){
            //退款通过
            OrderEntity orderEntity1 = orderEntityMapper.selectByPrimaryKey(refundReq.getOrderId());
            //获取其支付方式
            Byte payAuditType = orderEntity1.getPayAuditType();

            //将要退款的金额
            Long payApplyAmmount = orderEntity1.getPayApplyAmmount();

            //若是余额支付，返回余额
            if(payAuditType.equals(OrderConstant.BALANCE)){
                PurchaserAccountEntity purchaserAccountEntity =
                        purchaserAccountInfoMapper.selectAccountByOrderId(orderEntity1.getId());
                Long balance = purchaserAccountEntity.getMoneyLeft()+payApplyAmmount;
                PurchaserAccountEntity purchaserAccountEntity1 = new PurchaserAccountEntity();
                purchaserAccountEntity1.setId(purchaserAccountEntity.getId());
                purchaserAccountEntity1.setMoneyLeft(balance);
                purchaserAccountEntity1.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                p = purchaserAccountEntityMapper.updateByPrimaryKeySelective(purchaserAccountEntity1);

            }else{
                //非余额支付，将其原有支付金额清空
                orderEntity.setPayApplyAmmount(new Long(0));
                o = orderEntityMapper.updateByPrimaryKeySelective(orderEntity);
            }
        }else if(auditStatus.equals(OrderConstant.ORDER_REFUND_FAIL)){
            //退款失败

        }else if(auditStatus.equals(OrderConstant.ORDER_REFUND)){
            //协商一致 将订单状态恢复
            orderEntity.setStatus(refundRecordEntity1.getRefundStatus());
            i = orderEntityMapper.updateByPrimaryKeySelective(orderEntity);
        }
        result = ((p == 0 && o == 0 && i == 0)? new Result(ResultCodeEnum.APPEND_DATA_ERROR):new Result(ResultCodeEnum.OPERATION_SUCCESS));
        return result;
    }
}
